import 'package:flutter/services.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import 'textfield_input_formatter.dart';

///带删除按钮的输入框
class ClearText extends StatefulWidget {
  final TextEditingController? controller; //控制器
  final String? hintText; //提示文字
  final int? maxLength; //最大输入长度
  final double hintSize; //提示文字尺寸
  final List<TextInputFormatter>? inputFormatters;
  final FocusNode? focusNode; //监听器
  final double fontSize;
  final int? maxLines;
  final TextInputAction? textInputAction;
  final ValueChanged<String>? onSubmitted;
  final bool? enabled;
  final FontWeight? fontWeight;
  final FontWeight? hintFontWeight;
  final Color? inputColor; // 输入信息颜色
  final Color? hintColor; // 提示信息颜色
  final TextAlign inputAlign; // 输入信息对齐方式
  final double paddingEnd;
  final TextInputType? keyboardType;
  final double? maxSum;

  const ClearText({
    super.key,
    this.controller,
    this.hintText,
    this.maxLength,
    this.hintSize = 14.0,
    this.inputFormatters,
    this.focusNode,
    this.maxLines,
    this.fontSize = 14.0,
    this.textInputAction,
    this.onSubmitted,
    this.enabled,
    this.fontWeight,
    this.hintFontWeight,
    this.inputColor,
    this.hintColor = Colors.black26,
    this.inputAlign = TextAlign.start,
    this.paddingEnd = 5.0,
    this.keyboardType = TextInputType.text,
    this.maxSum,
  }); //输入类型

  @override
  ClearTextState createState() => ClearTextState();
}

class ClearTextState extends State<ClearText> {
  //用户名输入框控制器，此控制器可以监听用户名输入框操作
  bool? _isShowUserClear = false;

  void _focusNodeListener() {
    bool? isHasFocus = widget.focusNode?.hasFocus;

    if (isHasFocus != _isShowUserClear) {
      setState(() {
        _isShowUserClear = isHasFocus;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    widget.focusNode?.addListener(_focusNodeListener);
  }

  @override
  void dispose() {
    widget.focusNode?.removeListener(_focusNodeListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      enabled: widget.enabled,
      inputFormatters: widget.inputFormatters ??
          getInputFormat(widget.keyboardType,
              maxLength: widget.maxLength, maxSum: widget.maxSum),
      textAlignVertical: TextAlignVertical.center,
      textAlign: widget.inputAlign,
      controller: widget.controller,
      focusNode: widget.focusNode,
      //设置键盘类型
      keyboardType: widget.keyboardType,
      maxLength: widget.maxLength,
      maxLines: widget.maxLines,
      style: TextStyle(
        fontSize: widget.fontSize,
        fontWeight: widget.fontWeight,
        color: widget.inputColor,
      ),
      textInputAction: widget.textInputAction,
      onSubmitted: widget.onSubmitted,
      decoration: InputDecoration(
        counterText: "",
        border: const OutlineInputBorder(borderSide: BorderSide.none),
        contentPadding: const EdgeInsets.symmetric(vertical: 0),
        hintText: widget.hintText,
        hintStyle: TextStyle(
          color: widget.hintColor,
          fontSize: widget.hintSize,
          fontWeight: widget.hintFontWeight,
        ),
        suffixIconConstraints:
            BoxConstraints(maxHeight: 20.0, maxWidth: 20.0 + widget.paddingEnd),
        suffixIcon: _isShowUserClear!
            ? InkWell(
                child: Row(
                  children: [
                    ImageWidget.loadAssetImage("icon_input_clear",
                        width: 20.0, height: 20.0),
                    SizedBox(width: widget.paddingEnd),
                  ],
                ),
                onTap: () {
                  // 清空输入框内容
                  WidgetsBinding.instance.addPostFrameCallback(
                    (_) => widget.controller?.clear(),
                  );
                },
              )
            : SizedBox(width: widget.paddingEnd),
      ),
    );
  }
}
